package gui.panel_new.evaluation.student;

import enums.*;
import gui.panel_new.evaluation.exeptions.ExerciseNotBuiltException;
import gui.panel_new.evaluation.exeptions.PunctuationZeroException;
import gui.panel_new.evaluation.professor.CreateSylogismViolationPanel;
import j_util.Utiles;
import misc.BundlesProvider;
import model.Sylogism;
import model.eval.SylogismData;
import model.eval.TestExcercise;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ResourceBundle;
/*

    This file is part of Logica-Logike.

    Logica-Logike is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation version 3 of the License.

    Logica-Logike is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Logica-Logike.  If not, see <http://www.gnu.org/licenses/>.

 */
/**
 * Extiende de ExercisePanel, permite resolver ejercicios en los que se analizan las caracteristicas
 * de un silogismo: figura, terminos, extensiones;
 *
 * @author Jose Martin Rozanec;
 */
public class SylogismAnalysisPanel extends ExercisePanel {
    private javax.swing.JPanel bottomPanel;
    private javax.swing.JComboBox combo1;
    private javax.swing.JComboBox combo2;
    private javax.swing.JComboBox combo3;
    private javax.swing.JTextField conclusionField;
    private javax.swing.JLabel conclusionLabel;
    private javax.swing.JLabel convertirEnLabel;
    private javax.swing.JPanel cuadroPanel;
    private javax.swing.JLabel figuraLabel;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JTextField premisaMayorField;
    private javax.swing.JLabel premisaMayorLabel;
    private javax.swing.JTextField premisaMenorField;
    private javax.swing.JLabel premisaMenorLabel;
    private javax.swing.JLabel terminoMayorLabel;
    private javax.swing.JLabel terminoMedioLabel;
    private javax.swing.JLabel terminoMenorLabel;
    private javax.swing.JTextArea textArea;
    private JCheckBox checkBoxPrimera;
    private JCheckBox checkBoxSegunda;
    private JCheckBox checkBoxTercera;
    private JCheckBox checkBoxCuarta;
    private JTextField terminoMayorField;
    private JTextField terminoMedioField;
    private JTextField terminoMenorField;

    private JLabel addsPoints;
    private JLabel substractPoints;
    private JTextField addsPointsField;
    private JTextField substractPointsField;

    private int premisaLabelsStringLength;
    private ResourceBundle textBundle;

    private Sylogism sylogism;
    private FigureEnum figure;

    /**
     * Constructor;
     */
    public SylogismAnalysisPanel() {
        testPanelEnum = TestPanelEnum.ANALISIS_SILOGISMO;
        this.sylogism = new Sylogism();
        premisaLabelsStringLength = 10;
        textBundle = BundlesProvider.getBundle(URLEnum.TEXT_BUNDLE);
        initComponents();
    }

    /**
     * Inicializa componentes;
     */
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        cuadroPanel = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        premisaMayorField = new javax.swing.JTextField();
        premisaMayorLabel = new javax.swing.JLabel();
        convertirEnLabel = new javax.swing.JLabel();
        combo1 = new javax.swing.JComboBox();
        premisaMenorLabel = new javax.swing.JLabel();
        premisaMenorField = new javax.swing.JTextField();
        combo2 = new javax.swing.JComboBox();
        jSeparator1 = new javax.swing.JSeparator();
        conclusionLabel = new javax.swing.JLabel();
        conclusionField = new javax.swing.JTextField();
        combo3 = new javax.swing.JComboBox();
        jPanel2 = new javax.swing.JPanel();
        figuraLabel = new javax.swing.JLabel();
        terminoMayorLabel = new javax.swing.JLabel();
        terminoMedioLabel = new javax.swing.JLabel();
        terminoMenorLabel = new javax.swing.JLabel();
        bottomPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        textArea = new javax.swing.JTextArea();

        addsPoints = new JLabel(textBundle.getString(TextBundleEnum.SUMA.toString()));
        substractPoints = new JLabel(textBundle.getString(TextBundleEnum.RESTA.toString()));
        addsPointsField = new JTextField(7);
        addsPointsField.setEditable(false);
        substractPointsField = new JTextField(7);
        substractPointsField.setEditable(false);

        mainPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(
                new javax.swing.border.LineBorder(new java.awt.Color(204, 204, 204), 1, true),
                textBundle.getString(TextBundleEnum.SILOGISMO.toString())));

        cuadroPanel.setBackground(new java.awt.Color(255, 255, 255));
        cuadroPanel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(
                new javax.swing.border.LineBorder(new java.awt.Color(204, 204, 204), 1, true),
                textBundle.getString(TextBundleEnum.SILOGISMO.toString())));

        premisaMayorLabel.setText(Utiles.getFormattedString(
                textBundle.getString(TextBundleEnum.PREMISA_MAYOR.toString()), ""));
        premisaMayorLabel.setHorizontalTextPosition(SwingConstants.LEFT);
        premisaMayorLabel.setIcon(Utiles.createImageIcon(URLEnum.getReference(URLEnum.PREMISA_LARGO)));

        convertirEnLabel.setText(textBundle.getString(TextBundleEnum.EXTENSION.toString()));

        combo1.setModel(new javax.swing.DefaultComboBoxModel(new String[]{"-", "A", "E", "I", "O"}));

        premisaMenorLabel.setText(Utiles.getFormattedString(
                textBundle.getString(TextBundleEnum.PREMISA_MENOR.toString()), ""));
        premisaMenorLabel.setHorizontalTextPosition(SwingConstants.LEFT);
        premisaMenorLabel.setIcon(Utiles.createImageIcon(URLEnum.getReference(URLEnum.PREMISA_LARGO)));

        combo2.setModel(new javax.swing.DefaultComboBoxModel(new String[]{"-", "A", "E", "I", "O"}));

        jSeparator1.setForeground(new java.awt.Color(0, 0, 0));

        conclusionLabel.setText(Utiles.getFormattedString(
                textBundle.getString(TextBundleEnum.CONCLUSION.toString()), ""));
        conclusionLabel.setHorizontalTextPosition(SwingConstants.LEFT);
        conclusionLabel.setIcon(Utiles.createImageIcon(URLEnum.getReference(URLEnum.CONCLUSION_LARGO)));

        combo3.setModel(new javax.swing.DefaultComboBoxModel(new String[]{"-", "A", "E", "I", "O"}));

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(
                new java.awt.Color(204, 204, 204), 1, true), textBundle.getString(TextBundleEnum.FIGURA.toString())));

        checkBoxPrimera = new JCheckBox(textBundle.getString(TextBundleEnum.PRIMERA_FIGURA.toString()));
        checkBoxPrimera.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                figure = FigureEnum.FIRST;
            }
        });
        checkBoxSegunda = new JCheckBox(textBundle.getString(TextBundleEnum.SEGUNDA_FIGURA.toString()));
        checkBoxSegunda.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                figure = FigureEnum.SECOND;
            }
        });
        checkBoxTercera = new JCheckBox(textBundle.getString(TextBundleEnum.TERCERA_FIGURA.toString()));
        checkBoxTercera.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                figure = FigureEnum.THIRD;
            }
        });
        checkBoxCuarta = new JCheckBox(textBundle.getString(TextBundleEnum.CUARTA_FIGURA.toString()));
        checkBoxCuarta.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                figure = FigureEnum.FOURTH;
            }
        });

        ButtonGroup group = new ButtonGroup();
        group.add(checkBoxPrimera);
        group.add(checkBoxSegunda);
        group.add(checkBoxTercera);
        group.add(checkBoxCuarta);

        figuraLabel.setIcon(Utiles.createImageIcon(URLEnum.getReference(URLEnum.FIGURA_NONE)));

        terminoMayorField = new JTextField(10);
        terminoMedioField = new JTextField(10);
        terminoMenorField = new JTextField(10);

        terminoMayorLabel.setText(textBundle.getString(TextBundleEnum.TERMINO_MAYOR.toString()) + ":");
        terminoMedioLabel.setText(textBundle.getString(TextBundleEnum.TERMINO_MEDIO.toString()) + ":");
        terminoMenorLabel.setText(textBundle.getString(TextBundleEnum.TERMINO_MENOR.toString()) + ":");

        jPanel3 = createTerminosPanel();

//        bottomPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(
//                new java.awt.Color(204, 204, 204), 1, true),
//                textBundle.getString(TextBundleEnum.SUGERENCIAS_ANALISIS.toString())));
//        bottomPanel.setBorder(BorderFactory.createEtchedBorder());

        jScrollPane1.setViewportView(textArea);

        bottomPanel.getAccessibleContext().setAccessibleName(textBundle.getString(TextBundleEnum.ANALISIS.toString()));

        sizeTextFields();
        sizeTextAreas();
        layoutComponents();
    }

    /**
     * Realiza el layout de los componentes visuales;
     */
    private void layoutComponents() {
        setBackground(Color.WHITE);
        setLayout(new GridBagLayout());
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
        int x = 0;
        int y = 0;
        gridBagConstraints.insets = new Insets(5, 5, 5, 5);
        jPanel1.setLayout(new GridBagLayout());

        /*      PREMISAS     */
        jPanel1.add(premisaMayorLabel, gridBagConstraints);

        gridBagConstraints.gridx = (x += 7);
        jPanel1.add(convertirEnLabel, gridBagConstraints);

        gridBagConstraints.gridx = (x -= 7);
        gridBagConstraints.gridy = ++y;
        gridBagConstraints.gridwidth = 5;
        jPanel1.add(premisaMayorField, gridBagConstraints);

        gridBagConstraints.gridwidth = 1;
        gridBagConstraints.gridx = (x += 6);

        jPanel1.add(new JLabel(), gridBagConstraints);

        gridBagConstraints.gridx = (x += 1);
        jPanel1.add(combo1, gridBagConstraints);

        gridBagConstraints.gridx = (x -= 7);
        gridBagConstraints.gridy = ++y;
        jPanel1.add(premisaMenorLabel, gridBagConstraints);

        gridBagConstraints.gridwidth = 5;
        gridBagConstraints.gridy = ++y;
        jPanel1.add(premisaMenorField, gridBagConstraints);

        gridBagConstraints.gridwidth = 1;
        gridBagConstraints.gridx = (x += 6);
        jPanel1.add(new JLabel(), gridBagConstraints);

        gridBagConstraints.gridx = (x += 1);
        jPanel1.add(combo2, gridBagConstraints);

        gridBagConstraints.gridwidth = 7;
        gridBagConstraints.gridx = (x -= 7);
        gridBagConstraints.gridy = ++y;
        jSeparator1.setPreferredSize(new Dimension(550, 2));
        jPanel1.add(jSeparator1, gridBagConstraints);

        gridBagConstraints.gridwidth = 1;
        gridBagConstraints.gridy = ++y;
        jPanel1.add(conclusionLabel, gridBagConstraints);

        gridBagConstraints.gridy = ++y;
        gridBagConstraints.gridwidth = 5;
        jPanel1.add(conclusionField, gridBagConstraints);

        gridBagConstraints.gridwidth = 1;
        gridBagConstraints.gridx = (x += 6);
        jPanel1.add(new JLabel(), gridBagConstraints);

        gridBagConstraints.gridx = (x += 1);
        jPanel1.add(combo3, gridBagConstraints);

        jPanel1.setMinimumSize(new Dimension(655, 250));
        jPanel1.setPreferredSize(new Dimension(655, 250));
        x = 0;
        y = 0;
        /*      END OF PREMISAS     */

        /*      BUTTONS     */
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 3;
        add(jPanel1, gridBagConstraints);

        JPanel buttonsPanel = new JPanel(new GridLayout(1, 7));
        buttonsPanel.setBackground(Color.WHITE);

        gridBagConstraints.gridy = 1;
        add(buttonsPanel, gridBagConstraints);
        /*      END OF BUTTONS     */

        jPanel2.setMinimumSize(new Dimension(215, 175));
        jPanel2.setPreferredSize(new Dimension(215, 175));
        jPanel2.setLayout(new GridLayout(4, 1));
        jPanel2.add(checkBoxPrimera);
        jPanel2.add(checkBoxSegunda);
        jPanel2.add(checkBoxTercera);
        jPanel2.add(checkBoxCuarta);


        gridBagConstraints.gridwidth = 1;
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        add(jPanel2, gridBagConstraints);


        jPanel3.setMinimumSize(new Dimension(250, 175));
        jPanel3.setPreferredSize(new Dimension(250, 175));//430

        gridBagConstraints.gridwidth = 1;
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        add(jPanel3, gridBagConstraints);

        gridBagConstraints.gridx = 2;
        add(createScoringPanel(), gridBagConstraints);

        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;

        bottomPanel.setBackground(Color.WHITE);
        bottomPanel.setMinimumSize(new Dimension(655, 150));
        bottomPanel.setPreferredSize(new Dimension(655, 150));
        bottomPanel.add(new JLabel(Utiles.createImageIcon(URLEnum.getReference(URLEnum.LOGIKE_BOTTOM))));//jScrollPane1
        add(bottomPanel, gridBagConstraints);
    }

    private JPanel createScoringPanel() {
        JPanel scoringPanel = new JPanel();
        scoringPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),
                textBundle.getString(TextBundleEnum.PUNTAJE.toString())));
        scoringPanel.setLayout(new GridBagLayout());
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.insets = new Insets(5, 5, 5, 5);
        scoringPanel.add(addsPoints, gridBagConstraints);
        gridBagConstraints.gridx = 1;
        scoringPanel.add(addsPointsField, gridBagConstraints);
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridx = 0;
        scoringPanel.add(substractPoints, gridBagConstraints);
        gridBagConstraints.gridx = 1;
        scoringPanel.add(substractPointsField, gridBagConstraints);
        scoringPanel.setBackground(Color.WHITE);
        return scoringPanel;
    }

    private JPanel createTerminosPanel() {
        JPanel panel = new JPanel();
        panel.setBackground(new java.awt.Color(255, 255, 255));
        panel.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(
                new java.awt.Color(204, 204, 204), 1, true), textBundle.getString(TextBundleEnum.TERMINOS.toString())));
        panel.setLayout(new GridBagLayout());
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.insets = new Insets(6, 5, 5, 5);
        panel.add(terminoMayorLabel, gridBagConstraints);
        gridBagConstraints.gridx = 1;
        panel.add(terminoMayorField, gridBagConstraints);
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridx = 0;
        panel.add(terminoMedioLabel, gridBagConstraints);
        gridBagConstraints.gridx = 1;
        panel.add(terminoMedioField, gridBagConstraints);
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridx = 0;
        panel.add(terminoMenorLabel, gridBagConstraints);
        gridBagConstraints.gridx = 1;
        panel.add(terminoMenorField, gridBagConstraints);
        return panel;
    }

    /**
     * Determina el tamano de los textFields;
     */
    private void sizeTextFields() {
        premisaMayorField.setColumns(45);
        premisaMayorField.setMinimumSize(new Dimension(550, 20));
        premisaMayorField.setEditable(false);
        premisaMenorField.setColumns(45);
        premisaMenorField.setMinimumSize(new Dimension(550, 20));
        premisaMenorField.setEditable(false);
        conclusionField.setColumns(45);
        conclusionField.setMinimumSize(new Dimension(550, 20));
        conclusionField.setEditable(false);
    }

    /**
     * Determina el tamano de los textAreas;
     */
    private void sizeTextAreas() {
        textArea.setColumns(58);
        textArea.setEditable(false);
        textArea.setRows(7);
        textArea.setPreferredSize(new Dimension(550, 110));
    }

    /**
     * Analiza el silogismo;
     */
    private void analize() {
        textArea.setText(sylogism.analize());
    }

    /**
     * Permite setear el silogismo;
     *
     * @param sylogism
     */
    private void setSylogism(Sylogism sylogism) {
        System.out.println("Setting sylogism...");//todo delete
        this.sylogism = sylogism;
        if (sylogism.getPremisa(PremisaTypeEnum.FIRST) != null) {
            premisaMayorField.setText(sylogism.getPremisa(PremisaTypeEnum.FIRST).toString());
        }
        if (sylogism.getPremisa(PremisaTypeEnum.SECOND) != null) {
            premisaMenorField.setText(sylogism.getPremisa(PremisaTypeEnum.SECOND).toString());
        }
        if (sylogism.getPremisa(PremisaTypeEnum.CONCLUSION) != null) {
            conclusionField.setText(sylogism.getPremisa(PremisaTypeEnum.CONCLUSION).toString());
        }
        analize();
    }

    public TestExcercise getExercise() throws ExerciseNotBuiltException, PunctuationZeroException {
        SylogismData data = (SylogismData) testExercise.getStudentsSolution();
        if (!ExtensionEnum.valueOf((String) combo1.getSelectedItem()).equals("-")) {
            data.setPremisaMayorExtension(ExtensionEnum.valueOf((String) combo1.getSelectedItem()));
        }
        if (!ExtensionEnum.valueOf((String) combo2.getSelectedItem()).equals("-")) {
            data.setPremisaMenorExtension(ExtensionEnum.valueOf((String) combo2.getSelectedItem()));
        }
        if (!ExtensionEnum.valueOf((String) combo3.getSelectedItem()).equals("-")) {
            data.setConclusionExtension(ExtensionEnum.valueOf((String) combo3.getSelectedItem()));
        }
        data.setFigura(getFigure());
        data.setTerminoMayor(terminoMayorField.getText());
        data.setTerminoMedio(terminoMedioField.getText());
        data.setTerminoMenor(terminoMenorField.getText());
        return testExercise;
    }

    public void setExercise(TestExcercise testExercise) {
        this.testExercise = testExercise;
        setSylogism((Sylogism) testExercise.getExcerciseData());
        System.out.println("" + "" + testExercise.getCorrectScore());
        addsPointsField.setText("" + testExercise.getCorrectScore());//todo ver!
        substractPointsField.setText("" + testExercise.getIncorrectScore());//todo ver!
    }

    private FigureEnum getFigure() {
        return figure;
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("SilogismoPanel");
        frame.setContentPane(new CreateSylogismViolationPanel(new JButton(), new JButton(), new JFileChooser()));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}
